<?php

// No direct access to this file
defined('_JEXEC') or die('Restricted access');
JTable::addIncludePath(JPATH_COMPONENT.DIRECTORY_SEPARATOR.'tables');

class KetnoiModelUsergroup extends JModelLegacy
{
	public function getTable($type = 'Usergroup', $prefix = 'KetnoiTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}
	
	function loadGridData($limitstart,$limit,$where)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("*");
        //$query->from("#__user_group");
        $query->from("#__usergroups");
        $query->where("is_client = 1");
        $query->where($where);
        $query .= " order by id DESC ";
        $query .= " limit $limitstart,$limit";  
        $db->setQuery($query);
        $result = $db->loadObjectList();
        return $result; 
    }
    
	function loadAllGridData($where)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("count(*)");
        //$query->from("#__user_group");
        $query->from("#__usergroups");
        $query->where("is_client = 1"); 
        $query->where($where); 
        $db->setQuery($query);
        $result = $db->loadResult();
        return $result; 
    }
    
    public function getAllGroup()
    {
    	$db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("*");
        //$query->from("#__user_group");
        $query->from("#__usergroups");
        $query->where("is_client = 1");
        $query->where("is_delete = 0");
        $db->setQuery($query);
        $result = $db->loadObjectList();
        return $result;
    }
    
    function save()
    {
    	global $app;
    	$table = $this->getTable();
    	$post = JRequest::get("post");
    	$post["is_client"] = 1;
    	$post["parent_id"] = 1;
    	$isNew = empty($post["id"]);
		$table = Utility::save($table, $post);
		$id = $table->id;
		
		//set site login
		/*
		if($isNew)
		{
			
			$db = JFactory::getDbo();
			$query = "select rules from #__assets where id = 1";
			$db->setQuery($query);
			$result = $db->loadResult();
			$arr = json_decode($result,true);
			
			$login = $arr["core.login.site"];
			$login[$id] = 1;
		
			$arr["core.login.site"] = $login;
			$str_update = json_encode($arr);
			$query = "update #__assets set rules = '$str_update' where id = 1";
			$db->setQuery($query);
			$db->query();
		}*/
		
		return $table;
    }
    
    function checkTenExists()
    {
    	$ten = JRequest::getVar("tennhom");
    	$oldval = JRequest::getVar("oldval","");
    	if(strtolower($ten) == strtolower($oldval))
    	{
    		return false;
    	}
    	$db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("count(*)");
        //$query->from("#__user_group");
        $query->from("#__usergroups");
        //$query->where("is_delete = 0 and tennhom = '$ten'");
        $query->where("is_delete = 0 and is_client = 1 and title = '$ten'");
        $db->setQuery($query);
        $result = $db->loadResult();
        return $result > 0;
    }
    
	function loadData($id)
	{
		$table = $this->getTable();
		$table->load($id);
		return $table;
	}
	
	function delete()
	{
		$id = JRequest::getVar("id");
		$db = JFactory::getDbo();
		//$query = "update #__user_group set is_delete = 1 where id = $id";
		$query = "update #__usergroups set is_delete = 1 where id = $id";
		$db->setQuery($query);
		$db->query();
	}
}
